package com.bto.solar.work.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器的属性配置
 *
 */
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {

    /**
     * 把Interceptor这个实现类交给spring进行管理-------避坑！！！
     * @return
     */
    @Bean
    public LoginInterceptor getAdminInterceptor(){
        return new LoginInterceptor();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .allowCredentials(true).maxAge(3600);

    }

    /**
     * 重写addInterceptors()实现拦截器
     * 配置：要拦截的路径以及不拦截的路径
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册Interceptor拦截器(Interceptor这个类是我们自己写的拦截器类)
        InterceptorRegistration registration = registry.addInterceptor(getAdminInterceptor());
        //addPathPatterns()方法添加需要拦截的路径
        //所有路径都被拦截
        registration.addPathPatterns("/**");
        //excludePathPatterns()方法添加不拦截的路径
        //添加不拦截路径
        registration.excludePathPatterns(
                //APP登录
                "/login",
                //WEB登录
                "/login/web",
                //刷新token
                "/login/refresh",
                //退出登录
                "/login/out",
                //发送短信
                "/login/send-sms",
                //法大大回调
                "/fdd/**",
                //sto
                "/sto/**",
                //越秀回调
                "/approval/sync-approval/lease/status",
                //注册的公司列表
                "/company/list",
                //测试
                "/excel-download/**",
                //合同预览
                "/leaseContract/**",
                //发送短信
                "/sendshortMessage",
                //html静态资源
                "/**/*.html",
                //js静态资源
                "/**/*.js",
                //css静态资源
                "/**/*.css"
        );
    }
}
